package com.ruoyi.control.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.control.dto.AuxiliaryFanHistoryDto;
import com.ruoyi.control.dto.Mainventilator1HistoryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.AuxiliaryFanHistoryMapper;
import com.ruoyi.control.domain.AuxiliaryFanHistory;
import com.ruoyi.control.service.IAuxiliaryFanHistoryService;

/**
 * 局扇历史Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-03
 */
@Service
public class AuxiliaryFanHistoryServiceImpl extends ServiceImpl<AuxiliaryFanHistoryMapper, AuxiliaryFanHistory> implements IAuxiliaryFanHistoryService //修改
{
    @Autowired
    private AuxiliaryFanHistoryMapper auxiliaryFanHistoryMapper;

    @Autowired
    private RedisCache redisCache;
    /**
     * 查询局扇历史
     * 
     * @param id 局扇历史主键
     * @return 局扇历史
     */
    @Override
    public AuxiliaryFanHistory selectAuxiliaryFanHistoryById(String id)
    {
        return auxiliaryFanHistoryMapper.selectAuxiliaryFanHistoryById(id);
    }

    /**
     * 查询局扇历史列表
     * 
     * @param auxiliaryFanHistory 局扇历史
     * @return 局扇历史
     */
    @Override
    public List<AuxiliaryFanHistory> selectAuxiliaryFanHistoryList(AuxiliaryFanHistory auxiliaryFanHistory)
    {
        return auxiliaryFanHistoryMapper.selectAuxiliaryFanHistoryList(auxiliaryFanHistory);
    }

    /**
     * 新增局扇历史
     * 
     * @param auxiliaryFanHistory 局扇历史
     * @return 结果
     */
    @Override
    public int insertAuxiliaryFanHistory(AuxiliaryFanHistory auxiliaryFanHistory)
    {
        return auxiliaryFanHistoryMapper.insertAuxiliaryFanHistory(auxiliaryFanHistory);
    }

    /**
     * 修改局扇历史
     * 
     * @param auxiliaryFanHistory 局扇历史
     * @return 结果
     */
    @Override
    public int updateAuxiliaryFanHistory(AuxiliaryFanHistory auxiliaryFanHistory)
    {
        auxiliaryFanHistory.setUpdateTime(DateUtils.getNowDate());
        return auxiliaryFanHistoryMapper.updateAuxiliaryFanHistory(auxiliaryFanHistory);
    }

    /**
     * 批量删除局扇历史
     * 
     * @param ids 需要删除的局扇历史主键
     * @return 结果
     */
    @Override
    public int deleteAuxiliaryFanHistoryByIds(String[] ids)
    {
        return auxiliaryFanHistoryMapper.deleteAuxiliaryFanHistoryByIds(ids);
    }

    /**
     * 删除局扇历史信息
     * 
     * @param id 局扇历史主键
     * @return 结果
     */
    @Override
    public int deleteAuxiliaryFanHistoryById(String id)
    {
        return auxiliaryFanHistoryMapper.deleteAuxiliaryFanHistoryById(id);
    }

    /**
     * 7天局扇故障次数
     * @return
     */
    public List<AuxiliaryFanHistoryDto> getAuxiliaryFanTotalFault() {

        final String key = "jsGzKey";
        List<AuxiliaryFanHistoryDto> cachedValueList = redisCache.getCacheObject(key);

        List<AuxiliaryFanHistoryDto> list = auxiliaryFanHistoryMapper.getAuxiliaryFanTotalFault();


        if (list == null || list.isEmpty()) {
            //list  为空
            List<AuxiliaryFanHistoryDto> defaultList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                AuxiliaryFanHistoryDto abnormalCountDto = new AuxiliaryFanHistoryDto();
                abnormalCountDto.setCount("0");
                defaultList.add(abnormalCountDto);
            }
            List<String> collect = defaultList.stream().map(objInList -> {
                String abnormalCount = objInList.getCount();
                return abnormalCount;
            }).collect(Collectors.toList());
            redisCache.setCacheObject(key, collect);
        } else {
            List<AuxiliaryFanHistoryDto> resultList = new ArrayList<>(list);
            while (resultList.size() < 7) {
                AuxiliaryFanHistoryDto abnormalCountDto = new AuxiliaryFanHistoryDto();
                abnormalCountDto.setCount("0");
                if (abnormalCountDto.getUpdateDate() != null) {
                    abnormalCountDto.setUpdateDate(abnormalCountDto.getUpdateDate());
                }
                resultList.add(abnormalCountDto);
            }

            redisCache.setCacheObject(key, resultList);

        }

        return cachedValueList;

    }

    public List<AuxiliaryFanHistoryDto> getAuxiliaryFanPowerValueList(String str) {
        String[] keys = {"jsGhKey1", "jsGhKey2", "jsGhKey3", "jsGhKey4","jsGhKey5", "jsGhKey6",
                "jsGhKey7", "jsGhKey8","jsGhKey9", "jsGhKey10", "jsGhKey11"};
        String[] prefixes = {"1#", "2#", "3#", "4#","5#", "6#", "7#", "8#","9#", "10#", "11#"};

        int index = Integer.parseInt(str) - 1;
        String key = keys[index];
        String dy = prefixes[index] + "主机电压";
        String dl = prefixes[index] + "主机电流";

        List<AuxiliaryFanHistoryDto> cachedValueList = redisCache.getCacheObject(key);
        if (cachedValueList != null) {
            return cachedValueList;
        }

        List<AuxiliaryFanHistoryDto> list = auxiliaryFanHistoryMapper.getAuxiliaryFanPowerValueList(dy, dl);
        List<AuxiliaryFanHistoryDto> processedList = list.stream().map(objInList -> {
            Double avgPower = objInList.getAvgPower();
            Double consumptionValue = avgPower * 60 * 60;
            objInList.setConsumptionValue(Double.valueOf(String.format("%.2f", consumptionValue)));
            return objInList;
        }).collect(Collectors.toList());

        redisCache.setCacheObject(key, processedList);
        return processedList;
    }
}
